package com.spring.cloud.web;

import com.github.pagehelper.PageInfo;
import com.spring.cloud.entity.User;
import com.spring.cloud.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(value = "用户管理Controller",tags = "用户管理Controller")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "ID查询用户", notes = "ID查询用户")
    @GetMapping("/getUser/{id}")
    public User getUser(@PathVariable("id") Integer id){
        return userService.getUser(id);
    }

    @ApiOperation(value = "查询所有用户", notes = "查询所有用户")
    @GetMapping("/getUserList")
    public PageInfo<User> getUserList(@ApiParam(value = "第几页",required = false)@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                      @ApiParam(value = "多少个",required = false)@RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize
    ){
        return userService.getUserList(pageNum,pageSize);
    }

    /**
     * 用户注册
     * @param user
     * @param
     * @return
     */
    @ApiOperation(value = "新增用户名", notes = "新增用户名")
    @PostMapping("/register")
    public ResponseEntity<Void> register(@Valid User user) {
        userService.register(user);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据用户名和密码查询用户
     * @param username
     * @param password
     * @return
     */
    @ApiOperation(value = "根据用户名查询", notes = "根据用户名查询")
    @GetMapping("/query")
    public ResponseEntity<User> queryUser(@RequestParam("username") String username, @RequestParam("password") String password) {
        return ResponseEntity.ok(userService.queryUser(username, password));
    }

}
